﻿using NB.MGJ.Common.Helper;
using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Model.Model;
using NB.MGJ.Service;
using NB.MGJ.Service.AntiFake;
using NB.MGJ.Service.Origins;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public partial  class TraceSourceController : BaseController
    {

      
        public QueryStatisticsService _qsService = new QueryStatisticsService();
        private DemoService _demoService = new DemoService();
        private BasicOriginModuleService _basicOriginModule;
        public TraceSourceController()
        {
            _demoService = new DemoService();
            _basicOriginModule = new BasicOriginModuleService();
        }


        #region 基础自建模块
        public ActionResult BasicOriginModule()
        {
            return View();
        }

        public ActionResult GetUserAllBasicOriginModuleTable()
        {
            return Json(_basicOriginModule.GetBasicModule(LogOnUser.UserID, LogOnUser.EnterpriseId));
        }

        /// <summary>
        /// 
        /// </summary>
        public JsonResult AddBasicOriginModuleTable(string tablename)
        {
            if (string.IsNullOrWhiteSpace(tablename))
            {
                throw new ArgumentNullException("tablename");
            }
            var _model = new Origins_BasicModule
            {
                CreateDate = DateTime.Now,
                CreateEnterpriseId = LogOnUser.EnterpriseId,
                CreateEnterpriseName = LogOnUser.EnterpriseName,
                CreateUserId = LogOnUser.UserID,
                CreateUserName = LogOnUser.LoginName,
                TableChineseName = tablename
            };
            _basicOriginModule.AddOriginsBasicModule(_model);
            return Json(true);
        }


        [HttpPost]
        public JsonResult AddBasicOriginModuleTableColumn([System.Web.Http.FromBody]List<TableColumns> model)
        {
            var _tableName = Request["tableName"];
            var _cols = new JsonHelper().JSONToObject<List<TableColumns>>(Request.Form[0]).Distinct(new TableFeild()).ToList();
            return Json(_basicOriginModule.AddTableColumns(_tableName, _cols));
        }

        /// <summary>
        /// 删除表与主表记录
        /// </summary>
        public JsonResult DeleteOriginModule(int Id, string tableName)
        {
            _basicOriginModule.DeleteOriginBasicModule(Id, LogOnUser.UserID, LogOnUser.EnterpriseId, tableName);
            return Json(true);
        }

        //修改表名
        public JsonResult ModifyTableName(string tableName,string oldtableName, int Id)
        {
            _basicOriginModule.ModifyTableName(tableName, oldtableName,Id);
            return Json(true);
        }

        /// <summary>
        /// 获取表结构
        /// </summary>
        public JsonResult GetTablesStructure(string tablename)
        {
            return Json(_basicOriginModule.GetTablesStructure(tablename));
        }


        /// <summary>
        /// 删除表中列
        /// </summary>
        public JsonResult DeleteTableColumn(string tableName, string feildName)
        {
           return Json(_basicOriginModule.DeleteTableColumn(tableName, feildName));
        }

        /// <summary>
        /// 获取表中数据
        /// </summary>
        public JsonResult GetTableRowsData(string tableName, string where = null)
        {
            var _result = _basicOriginModule.GetTableRowsData(tableName, where);
            return Json(new JsonHelper().SerializeObject(_result));
        }


        /// <summary>
        /// 添加表-行数据
        /// </summary>
        public JsonResult AddTableRowsData(string rowData, string tableName, string Id = null)
        {
            var rs = rowData.Remove(rowData.Length - 1).Split(',');//注：后续
            //var rs = rowData.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            _basicOriginModule.UpdateTableRowsData(rs, tableName,Id);
            return Json(true);
        }


        /// <summary>
        /// 删除表行数据
        /// </summary>
        public JsonResult DeleteTableRowsData(string Id,string tableName)
        {
            _basicOriginModule.DeleteTableRowsData(Id, tableName);
            return Json(true);
        }


         ///<summary>
         ///获取mysql常用数据类型
         ///</summary>
        public JsonResult GetDBTypeList()
        {
            return Json(_basicOriginModule.GetDBTypeList());
        }


        #endregion


    }

    /// <summary>
    /// 排除相同名称字段
    /// </summary>
    internal class TableFeild : IEqualityComparer<TableColumns>
    {
        public bool Equals(TableColumns X, TableColumns Y)
        {
            return X.Field == Y.Field;
        }


        public int GetHashCode(TableColumns obj)
        {
            if (obj.Field == null)
                return 0;
            else
                return obj.Field.GetHashCode();
        }
    }
}